package com.cy.aircompressor.main.view.chart;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cy.aircompressor.databinding.ItemLegendViewBinding;
import com.cy.aircompressor.entity.linechart.LineLegend;

public class LegendView extends FrameLayout {
    private Context mContext;
    private ItemLegendViewBinding mBinding;

    public LegendView(@NonNull Context context) {
        this(context, null);
    }

    public LegendView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LegendView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public LegendView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.mContext = context;
        initView();
    }

    private void initView() {
        mBinding = ItemLegendViewBinding.inflate(LayoutInflater.from(getContext()));
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        addView(mBinding.getRoot(), layoutParams);
    }

    public void setLegend(LineLegend lineLegend) {
        mBinding.labeledName.setText(lineLegend.getName());
        mBinding.labeledDot.getDelegate().setBackgroundColor(Color.parseColor(lineLegend.getColor()));
    }
}
